package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.PermissionAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {


    private IEmployeeService service;

    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @Autowired
    public void setService(IEmployeeService service) {
        this.service = service;
    }

    @PermissionAnnotation(name = "员工列表",expression = "employee.list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);
        PageInfo<Employee> pageResult = service.getPageInfo(qo);
        model.addAttribute("PageInfo",pageResult);
        return "employee/list";
    }

    @PermissionAnnotation(name = "员工删除",expression = "employee.delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            service.delete(id);
            service.deleteEmployeeAndRole(id);
        }
        return "redirect:/employee/list";
    }


    @PermissionAnnotation(name = "员工编辑",expression = "employee.input")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        model.addAttribute("departments",departmentService.selectAll());
        model.addAttribute("role",roleService.getAll());
        model.addAttribute("selected",service.selectRole(id));
        if (id!=null){
            Employee employee = service.selectOne(id);
            model.addAttribute("employee",employee);
        }
        return "employee/input";
    }

    @PermissionAnnotation(name = "员工保存",expression = "employee.saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId()!=null){
            service.deleteEmployeeAndRole(employee.getId());
            service.update(employee);
            if (ids!=null){
            for (Long id : ids) {
                    service.insertEmployeeAndRole(employee.getId(),id);
                }
            }
        }else {
            service.insert(employee);

            if (ids!=null) {
                for (Long id : ids) {
                    service.insertEmployeeAndRole(employee.getId(), id);
                }
            }
        }
        return "redirect:/employee/list";
    }


    @RequestMapping("/exportXls")
    public void Export(HttpServletResponse response) throws IOException {

        //解决中文异常
        String s = new String("员工通讯录.xls".getBytes("utf-8"), "ISO-8859-1");
        //设置响应头
        response.setHeader("Content-Disposition","attachment;filename="+s);
        service.exportXls(response.getOutputStream());
    }

    @RequestMapping("importXls")
    public String importXls(MultipartFile file) throws IOException {
        service.importXls(file);
        return "redirect:/employee/list";
    }
}
